package com.lesports.qmt.sbc.converter;

import com.alibaba.fastjson.JSON;
import com.lesports.api.common.ImageUrlExt;
import com.lesports.qmt.mvc.QmtVoConverter;
import com.lesports.qmt.sbc.model.News;
import com.lesports.qmt.sbc.model.NewsImage;
import com.lesports.qmt.sbc.param.NewsParam;
import com.lesports.qmt.sbc.vo.NewsVo;
import com.lesports.utils.math.LeNumberUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by denghui on 2016/10/31.
 */
@Component
public class NewsVoConverter implements QmtVoConverter<News, NewsVo> {

    private static final Logger LOG = LoggerFactory.getLogger(NewsVoConverter.class);

    @Override
    public NewsVo toVo(Object object) {
       NewsParam news = (NewsParam) object;
        NewsVo newsVo = new NewsVo();
        if(news.getId() > 0){
            newsVo.setId(news.getId());
        }
        newsVo.setTitle(news.getTitle());
        newsVo.setType(news.getType());
        newsVo.setChannel(news.getChannel());
        newsVo.setColumn(news.getColumn());
        newsVo.setStatus(news.getStatus());
        newsVo.setSuperscript(news.getSuperscript());
        newsVo.setContent(news.getContent());
        newsVo.setDesc(news.getDesc());
        newsVo.setShareDesc(news.getShareDesc());
        newsVo.setShortTitle(news.getShortTitle());
        newsVo.setResource(news.getResource());
        newsVo.setSourceUrl(news.getSourceUrl());
        newsVo.setImageDesc(news.getImageDesc());
        newsVo.setCoverImage(news.getCoverImage());
        //封面图片不为空
        if(news.getImages() != null){
            Map<String, ImageUrlExt> images = JSON.parseObject(news.getImages(), Map.class);
            //封面图数量校验
            if(MapUtils.isEmpty(images)) {
                LOG.error("invalid number of cover image: {}", images);
                throw new IllegalArgumentException("invalid number of cover images");
            }
            newsVo.setImages(images);
        }
        newsVo.setAudit(news.getAudit());

        //图片
//        if (news.getType() == NewsType.IMAGE_ALBUM) {
//            if (news.getImages().contains("\"id\":")) {
//                //fix fastjson error
//                news.setImages(news.getImages().replaceAll("\"id\":", "\"helperId\":"));
//            }
//            List<NewsImage> newsImages = JSON.parseArray(news.getImages(), NewsImage.class);
//            //封面图数量校验
//            if(!checkImageAlbumCover(newsImages)) {
//                LOG.error("invalid number of cover images: {}", newsImages);
//                throw new IllegalArgumentException("invalid number of cover images");
//            }
//            newsVo.setImages(Lists.transform(newsImages, new Function<NewsImage, NewsImage>() {
//                @Nullable
//                @Override
//                public NewsImage apply(@Nullable NewsImage input) {
//                    //只复制前端可编辑的属性
//                    NewsImage newsImage = new NewsImage();
//                    newsImage.setId(input.getId());
//                    newsImage.setName(input.getName());
//                    newsImage.setDesc(input.getDesc());
//                    newsImage.setCover(input.getCover());
//                    newsImage.setAggCover(input.getAggCover());
//                    newsImage.setShowOrder(input.getShowOrder());
//                    newsImage.setImage(input.getImage());
//                    return newsImage;
//                }
//            }));
//        } else if (news.getType() == NewsType.RICHTEXT) {
//            Map<String, ImageUrlExt> coverImage = JSON.parseObject(news.getCoverImage(), Map.class);
//            //封面图数量校验
//            if(MapUtils.isEmpty(coverImage)) {
//                LOG.error("invalid number of cover image: {}", coverImage);
//                throw new IllegalArgumentException("invalid number of cover images");
//            }
//            newsVo.setImages(coverImage);
//        } else {
//            LOG.error("unknown news type: {}", news.getType());
//            throw new IllegalArgumentException("unknown news type");
//        }
        return newsVo;
    }

    private boolean checkImageAlbumCover(List<NewsImage> newsImages) {
        int listCoverCount = 0, aggCoverCount = 0;
        if (CollectionUtils.isNotEmpty(newsImages)) {
            for (NewsImage newsImage : newsImages) {
                if (LeNumberUtils.toBoolean(newsImage.getCover())) {
                    listCoverCount++;
                }
                if (LeNumberUtils.toBoolean(newsImage.getAggCover())) {
                    aggCoverCount++;
                }
            }
        }
        return listCoverCount == 3 && aggCoverCount == 1;
    }

    @Override
    public News copyEditableProperties(News existsEntity, NewsVo vo) {
        existsEntity.setId(vo.getId());
        existsEntity.setTitle(vo.getTitle());
        existsEntity.setType(vo.getType());
        existsEntity.setChannel(vo.getChannel());
        existsEntity.setColumn(vo.getColumn());
        existsEntity.setStatus(vo.getStatus());
        existsEntity.setSuperscript(vo.getSuperscript());
        existsEntity.setContent(vo.getContent());
        existsEntity.setDesc(vo.getDesc());
        existsEntity.setShareDesc(vo.getShareDesc());
        existsEntity.setShortTitle(vo.getShortTitle());
        existsEntity.setResource(vo.getResource());
        existsEntity.setSourceUrl(vo.getSourceUrl());
        return existsEntity;
    }

}
